//
//  PlatformDetector.swift
//  AppleAppDemo
//
//  Created by 刘忠华 on 2025/9/9.
//

import Foundation
import SwiftUI

/// 支持的平台类型
enum Platform: String, CaseIterable {
    case iOS = "iOS"
    case iPadOS = "iPadOS"
    case macOS = "macOS"
    case tvOS = "tvOS"
    
    /// 获取当前运行平台
    static var current: Platform {
        #if os(iOS)
        #if targetEnvironment(macCatalyst)
        return .macOS
        #else
        if UIDevice.current.userInterfaceIdiom == .pad {
            return .iPadOS
        } else {
            return .iOS
        }
        #endif
        #elseif os(macOS)
        return .macOS
        #elseif os(tvOS)
        return .tvOS
        #else
        return .iOS // 默认值
        #endif
    }
    
    /// 平台显示名称
    var displayName: String {
        return self.rawValue
    }
    
    /// 是否为移动设备
    var isMobile: Bool {
        return self == .iOS || self == .iPadOS
    }
    
    /// 是否为桌面设备
    var isDesktop: Bool {
        return self == .macOS
    }
    
    /// 是否为电视设备
    var isTV: Bool {
        return self == .tvOS
    }
    
    /// 是否支持触控
    var supportsTouch: Bool {
        return self.isMobile
    }
    
    /// 是否支持鼠标
    var supportsMouse: Bool {
        return self == .macOS || self == .iPadOS
    }
    
    /// 是否支持遥控器
    var supportsRemote: Bool {
        return self == .tvOS
    }
}

/// 平台检测工具类
struct PlatformDetector {
    /// 当前平台
    static let current = Platform.current
    
    /// 是否为iOS
    static var isIOS: Bool {
        return current == .iOS
    }
    
    /// 是否为iPadOS
    static var isIPadOS: Bool {
        return current == .iPadOS
    }
    
    /// 是否为macOS
    static var isMacOS: Bool {
        return current == .macOS
    }
    
    /// 是否为tvOS
    static var isTVOS: Bool {
        return current == .tvOS
    }
    
    /// 是否为移动设备
    static var isMobile: Bool {
        return current.isMobile
    }
    
    /// 是否为桌面设备
    static var isDesktop: Bool {
        return current.isDesktop
    }
    
    /// 是否为电视设备
    static var isTV: Bool {
        return current.isTV
    }
}

/// 平台特定的配置
struct PlatformConfig {
    /// 获取平台特定的字体大小
    static func fontSize(for platform: Platform, base: CGFloat = 16) -> CGFloat {
        switch platform {
        case .iOS:
            return base
        case .iPadOS:
            return base * 1.2
        case .macOS:
            return base * 1.1
        case .tvOS:
            return base * 1.5
        }
    }
    
    /// 获取平台特定的间距
    static func spacing(for platform: Platform, base: CGFloat = 16) -> CGFloat {
        switch platform {
        case .iOS:
            return base
        case .iPadOS:
            return base * 1.5
        case .macOS:
            return base * 1.2
        case .tvOS:
            return base * 2.0
        }
    }
    
    /// 获取平台特定的圆角半径
    static func cornerRadius(for platform: Platform, base: CGFloat = 8) -> CGFloat {
        switch platform {
        case .iOS:
            return base
        case .iPadOS:
            return base * 1.2
        case .macOS:
            return base * 0.8
        case .tvOS:
            return base * 1.5
        }
    }
    
    /// 获取平台特定的背景颜色
    static func backgroundColor(for platform: Platform) -> Color {
        #if os(tvOS)
        return Color.black.opacity(0.3)
        #elseif os(macOS)
        return Color(NSColor.controlBackgroundColor)
        #else
        return Color(.systemBackground)
        #endif
    }
    
    /// 获取平台特定的卡片背景颜色
    static func cardBackgroundColor(for platform: Platform) -> Color {
        #if os(tvOS)
        return Color.black.opacity(0.2)
        #elseif os(macOS)
        return Color(NSColor.controlBackgroundColor)
        #else
        return Color(.systemBackground)
        #endif
    }
}
